package com.iptv.grape.event.imp;

import com.google.gson.Gson;
import com.iptv.grape.BaseActivity;
import com.iptv.grape.bu.GrapeContext;
import com.iptv.grape.domain.Channel;
import com.iptv.grape.event.EventArgs;
import com.iptv.grape.event.JSRequest;
import com.iptv.grape.event.LiveWindowDataArgs;
import com.iptv.grape.event.data.LiveData;
import com.iptv.grape.event.msg.IntoChannelArgs;
import com.iptv.grape.event.msg.IntoChannelData;
import com.iptv.grape.event.proc.IWebViewMessageProc;
import com.iptv.grape.util.AppConstant;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.video.XMediaPlayer;

import org.greenrobot.eventbus.EventBus;
// 处理直接从H5推荐位界面上 进入全屏直播界面
public class IntoChannel implements IWebViewMessageProc {

    private IntoChannelArgs argsData;
    @Override
    public void init(JSRequest jsRequest) {
        try {
            //AppLog.getCurLog().out(jsRequest.getJson());
            IntoChannelData data = new Gson().fromJson(jsRequest.getJson(), IntoChannelData.class);
            argsData = data.getData();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public String procMessage(BaseActivity baseActivity, XMediaPlayer mediaPlayer) {
        // 找到对应的频道
        Channel channel = GrapeContext.curGrapeContext().findChannelByID(argsData.getChannelID());
        if(channel == null){
            AppLog.getCurLog().out("没有找到对应的频道,从当前的频道中进入全屏:" + argsData.getChannelID());
            channel = GrapeContext.curGrapeContext().getChannelDLinkList().getCurChannel();
        }
        // 先停止播放
        mediaPlayer.stop();
        // 全屏播放
        mediaPlayer.fullScreenPlay(baseActivity,channel.getPlayUrl());
        // 设置直播的播放历史
        GrapeContext.curGrapeContext().getChannelDLinkList().setChannelHis(channel);
        // 设置程序状态
        GrapeContext.curGrapeContext().setFocus(true);
        GrapeContext.curGrapeContext().setFullScreen(true);
        GrapeContext.curGrapeContext().setPlaying(true);
        // 设置退出全屏时 需要关闭播放器
        GrapeContext.curGrapeContext().setQuitFullScreenStopPlay(argsData.isClose());

        LiveData liveData = new LiveData(channel, "4", "23", "0");
        EventBus.getDefault().post(new EventArgs(AppConstant.DataLive, AppConstant.DataAcquisition, liveData));
        return "";
    }
}
